package cn.xshi.sys.service.impl;
import java.util.List;
import java.util.Map;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.util.JsonUtil;
import cn.xshi.common.util.StringUtil;
import cn.xshi.common.util.file.FileUtil;
import cn.xshi.sys.form.SysConstantForm;
import cn.xshi.sys.init.GlobalPersistentComponent;
import cn.xshi.sys.model.SysConstant;
import cn.xshi.sys.service.SysConstantService;
import cn.xshi.sys.dao.SysConstantDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import cn.xshi.common.base.BaseService;
import cn.xshi.common.util.ExceptionUtil;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * @Desc 常量
 * @Author 邓纯杰
 * @CreateTime 2024-04-24 09:38:12
 */
@Service
@Slf4j
public class SysConstantServiceImpl extends BaseService implements SysConstantService {

	@Resource
	private SysConstantDao sysConstantDao;

	@Resource
    GlobalPersistentComponent globalPersistentComponent;

	/**
	* 分页
	* @param sysConstantForm
	* @return
	*/
	public List<SysConstant> getSysConstantListByCondition(SysConstantForm sysConstantForm){
		try {
			return sysConstantDao.getSysConstantListByCondition(sysConstantForm);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 查询对象
	* @param id
	* @return
	*/
	public SysConstant getSysConstantById(String id){
		try {
			return sysConstantDao.getSysConstantById(id);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 添加
	* @param sysConstant
	* @return
	*/
	public int addSysConstant(SysConstant sysConstant){
		int i = 0;
		try {
			sysConstant.setCreateTime(getDate());
			sysConstant.setCreateId(getCurrentAccountId());
			i = sysConstantDao.addSysConstant(sysConstant);
			globalPersistentComponent.initSysConstant();
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 修改
	* @param sysConstant
	* @return
	*/
	public int updateSysConstant(SysConstant sysConstant){
		int i = 0;
		try {
			sysConstant.setUpdateId(getCurrentAccountId());
			sysConstant.setUpdateTime(getDate());
			i = sysConstantDao.updateSysConstant(sysConstant);
			globalPersistentComponent.initSysConstant();
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 删除
	* @param condition 
	* @return
	*/
	public int delSysConstant(Map<String,Object> condition){
		int i = 0;
		try {
			i = sysConstantDao.delSysConstant(condition);
			globalPersistentComponent.initSysConstant();
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	 * 查询常量集合
	 * @param condition
	 * @return
	 */
	public List<SysConstant> getSysConstantListAllByCondition(Map<String,Object> condition){
		try {
			return sysConstantDao.getSysConstantListAllByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 导出
	 * @param response
	 * @param id
	 */
	public void exportConstant(HttpServletResponse response, String id){
		try{
			if(StringUtil.isEmpty(id)){
				log.warn("未能获取id");
				throw new ExceptionUtil("未能获取id");
			}
			SysConstant sysConstant = sysConstantDao.getSysConstantById(id);
			if(null == sysConstant){
				log.warn("未能获取到常量信息");
				throw new ExceptionUtil("未能获取到常量信息");
			}
			String json = JsonUtil.toFastJson(sysConstant);
			FileUtil.exportJsonFile(response,json,sysConstant.getName()+"-"+sysConstant.getName()+".json");
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 导入
	 * @param request
	 * @param response
	 * @param multipartFile
	 * @return
	 */
	public BaseResult importConstant(HttpServletRequest request, HttpServletResponse response, MultipartFile multipartFile){
		String jsonText = FileUtil.readJson(multipartFile);
		if(StringUtil.isEmpty(jsonText)){
			return BaseResult.fail("导入内容为空！");
		}
		SysConstant sysConstant = JsonUtil.fromAliFastJson(jsonText,SysConstant.class);
		if(null == sysConstant){
			return BaseResult.fail("未能解析到常量信息！");
		}
		if(StringUtil.isEmpty(sysConstant.getId())){
			return BaseResult.fail("未能解析到常量id！");
		}
		if(StringUtil.isEmpty(sysConstant.getName())){
			return BaseResult.fail("未能解析到常量名称！");
		}
		SysConstant constant = getSysConstantById(sysConstant.getId());
		if(null == constant){//新增
			addSysConstant(sysConstant);
		}else{
			sysConstant.setUpdateId(getCurrentAccountId());
			sysConstant.setUpdateTime(getDate());
			updateSysConstant(sysConstant);
		}
		return BaseResult.success();
	}
}
